using System.Threading;

namespace muduoDotNet
{
    /// <summary>
    /// ThreadLocalEventLoop类，用于在线程本地存储中保存EventLoop对象
    /// </summary>
    public class ThreadLocalEventLoop
    {
        private static readonly ThreadLocal<EventLoop> _threadLocalLoop = new ThreadLocal<EventLoop>();

        /// <summary>
        /// 获取当前线程的EventLoop
        /// </summary>
        public static EventLoop GetEventLoop()
        {
            return _threadLocalLoop.Value;
        }

        /// <summary>
        /// 设置当前线程的EventLoop
        /// </summary>
        /// <param name="loop">要设置的EventLoop</param>
        public static void SetEventLoop(EventLoop loop)
        {
            _threadLocalLoop.Value = loop;
        }

        /// <summary>
        /// 清除当前线程的EventLoop
        /// </summary>
        public static void ClearEventLoop()
        {
            _threadLocalLoop.Value = null;
        }

        /// <summary>
        /// 检查当前线程是否有EventLoop
        /// </summary>
        /// <returns>如果有则返回true</returns>
        public static bool HasEventLoop()
        {
            return _threadLocalLoop.Value != null;
        }
    }
}